/***********************************************************
		Group			:		G2
		Project         :       Big Table
		File Name       :       bigt.java
		Author			:		
		Purpose         :       
		Change History  :
 ***********************************************************/
package BigT;
// CHange by Preetika Tyagi
import global.Convert;
import global.GlobalConst;
import global.MID;
import global.SystemDefs;
import heap.HFBufMgrException;
import heap.HFDiskMgrException;
import heap.HFException;
import heap.Heapfile;
import heap.InvalidMapSizeException;
import heap.InvalidSlotNumberException;
import heap.Scan;
import heap.SpaceNotAvailableException;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class bigt {
	String pathName = null;

	public void bigt(String name, int type, int numBuf) {
		// Initializes the big table
		// type defines the indexing
		pathName = "lib//" + GlobalConst.nameBigTDB + name + ".BigTDatabase-db";
		GlobalConst.pCounter.initialize();
		SystemDefs sysdef = new SystemDefs(pathName,
				GlobalConst.bigTDabaseSize, numBuf, "Clock");
	}

	public void bigt(String name, int type) {
		// Initializes the big table
		// type defines the indexing
		pathName = "lib//" + GlobalConst.nameBigTDB + name + ".BigTDatabase-db";
		GlobalConst.pCounter.initialize();
		SystemDefs sysdef = new SystemDefs(pathName,
				GlobalConst.bigTDabaseSize, GlobalConst.NUMBUF, "Clock");
	}

	void deleteBigt() {

		File DBfile = new File(pathName);
		DBfile.delete();

		// delete the bigtable from database
	}

	public int getRowCnt() throws HFException, HFBufMgrException,
			HFDiskMgrException, IOException, InvalidMapSizeException {
		int rowCount = 0;
		// return the no of maps

		Heapfile f = new Heapfile(GlobalConst.BigTHeapFile);
		Scan scan = null;
		MID mid1 = new MID();
		try {
			scan = f.openScan();
		} catch (InvalidMapSizeException e) {
			e.printStackTrace();
		}

		boolean done = false;
		int i, count = 0;
		String marks[] = new String[5000];
		try {

			String str;
			Map map1 = null;

			map1 = scan.getNext(mid1);

			byte[] data = null;
			data = new byte[map1.getLength()];
			data = map1.getMapByteArray();
			String name = Convert.getStrValue(0, data, 32);
			marks[count] = name;
			count++;
			while (done != true) {

				map1 = scan.getNext(mid1);
				if (map1 == null) {
					done = true;
					break;
				}
				data = null;
				data = new byte[map1.getLength()];
				data = map1.getMapByteArray();
				name = Convert.getStrValue(0, data, 32);

				for (i = 0; i < count;) {
					if (!marks[i].equals(name)) {
						i = i + 1;
						if (i == count) {
							marks[count] = name;
							count = count + 1;
							break;
						}
					} else {
						break;
					}
				}

			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		rowCount = count;

		return rowCount;
	}

	public int getColumnCnt() throws HFException, HFBufMgrException,
			HFDiskMgrException, IOException, InvalidMapSizeException {
		int columnCount = 0;
		// return the no of maps

		Heapfile f = new Heapfile(GlobalConst.BigTHeapFile);
		Scan scan = null;
		MID mid1 = new MID();
		// f = new Heapfile("test1.in");
		try {
			scan = f.openScan();
		} catch (InvalidMapSizeException e) {
			e.printStackTrace();
		}

		boolean done = false;
		int i, count = 0;
		String marks[] = new String[5000];
		try {

			String str;
			Map map1 = null;
			map1 = scan.getNext(mid1);
			byte[] data = null;
			data = new byte[map1.getLength()];
			data = map1.getMapByteArray();
			String name = Convert.getStrValue(32, data, 32);
			marks[count] = name;
			count++;
			while (done != true) {

				map1 = scan.getNext(mid1);
				if (map1 == null) {
					done = true;
					break;
				}

				data = null;
				data = new byte[map1.getLength()];
				data = map1.getMapByteArray();
				name = Convert.getStrValue(32, data, 32);
				for (i = 0; i < count;) {
					if (!marks[i].equals(name)) {
						i = i + 1;
						if (i == count) {
							marks[count] = name;
							count = count + 1;
							break;
						}
					} else {
						break;
					}
				}
				// System.out.println(count);
				/*
				 * if (map1 == null) { done = true; break; }
				 */

			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		columnCount = count;

		return columnCount;
	}

	public int getMapCnt() throws HFException, HFBufMgrException,
			HFDiskMgrException, IOException, InvalidMapSizeException {
		int mapCount = 0;
		// return the no of maps

		Heapfile f = new Heapfile(GlobalConst.BigTHeapFile);
		Scan scan = null;
		MID mid1 = new MID();
		f = new Heapfile(GlobalConst.BigTHeapFile);
		try {
			scan = f.openScan();
		} catch (InvalidMapSizeException e) {
			e.printStackTrace();
		}

		boolean done = false;
		int i, count = 0;
		String marks[] = new String[5000];
		try {

			String str;
			Map map1 = null;
			map1 = scan.getNext(mid1);
			byte[] data = null;
			data = new byte[map1.getLength()];
			data = map1.getMapByteArray();
			String name = Convert.getStrValue(0, data, 32);
			marks[count] = name;
			count++;
			while (done != true) {

				map1 = scan.getNext(mid1);
				if (map1 == null) {
					done = true;
					break;
				}
				data = null;
				data = new byte[map1.getLength()];
				data = map1.getMapByteArray();
				name = Convert.getStrValue(0, data, 32);
				count++;
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		mapCount = count;

		return mapCount;
	}

	public MID insertMap(Heapfile f, byte[] mapPtr) {
		MID mid = null;
		// inserts map into big table and return its MID. Also ensures 3 maps at
		// a time for a row
		// Heapfile hf = null;
		try {
			mid = f.insertRecord(mapPtr);
		} catch (InvalidSlotNumberException e) {
			e.printStackTrace();
		} catch (InvalidMapSizeException e) {
			e.printStackTrace();
		} catch (SpaceNotAvailableException e) {
			e.printStackTrace();
		} catch (HFException e) {
			e.printStackTrace();
		} catch (HFBufMgrException e) {
			e.printStackTrace();
		} catch (HFDiskMgrException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return mid;
	}

	Stream openStrem(int orderType, String rowFilter, String columnFilter,
			String valueFilter) {
		Stream mapStream = null;
		// initializes the stream of maps fetched on the basis of filters
		return mapStream;
	}

	/*
	 * public static void main(String args[]) { bigt bigtObj = new bigt();
	 * bigtObj.bigt("BigTable", 1); //
	 * System.out.println(bigtObj.bigt("BigTable", 1)); // bigtObj.deleteBigt();
	 * System.out.println("we are here"); }
	 */
	Stream openStream(bigt bigtObj, String bigTName, int type, int orderType,
			String rowFilter, String columnFilter, String valueFilter,
			int numBuf) {
		Stream stream = new Stream(bigtObj, bigTName, type, orderType,
				rowFilter, columnFilter, valueFilter, numBuf);
		return stream;
	}
}
